package my.netty.channel;

import my.netty.exception.ChannelException;

import java.nio.channels.spi.AbstractSelectableChannel;

public abstract class AbstractChannel implements Channel {

    private final ChannelPipeline pipeline;

    protected EventLoop eventLoop;

    public AbstractChannel() {
        this.pipeline = new ChannelPipeline(this);
    }

    public ChannelPipeline pipeline() {
        return pipeline;
    }

    public EventLoop eventLoop() {
        return eventLoop;
    }

    @Override
    public void close() {
        try {
            javaChannel().close();
        } catch (Throwable e) {
            throw new ChannelException("close channel", e);
        }
    }

    protected abstract AbstractSelectableChannel javaChannel();
}
